package jemuillot.BiohazardPanelPuzzle;

import java.io.IOException;

import jemuillot.pkg.Utilities.LocalizedPath;
import jemuillot.pkg.Utilities.SelfUpdater;

import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.audio.sound.SoundFactory;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.view.RenderSurfaceView;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class BiohazardPanelPuzzle extends BaseGameActivity implements
		BiohazardPanelPuzzleConsts {

	private Camera camera;

	TextureRegion backgroundRegion;
	private Texture backgroundTexture;
	private Texture titleTexture;
	Font font;
	Font fontHalf;
	private Texture fontTexture;

	TextureRegion titleRegion;

	private Texture panelTexture;

	TextureRegion panelRegion;

	private Texture lightOnTexture;

	private Texture lightOffTexture;

	TextureRegion lightOnRegion;

	TextureRegion lightOffRegion;

	private Texture fontHalfTexture;

	TextureRegion panelRegionX;

	private Texture panelTextureX;

	Sound sfxToggle;

	Sound sfxDone;

	BiohazardPanelPuzzleScene gameScene;

	int maxLevel;

	Sound sfxRejected;

	private LocalizedPath titleLp;
	
	public SelfUpdater updater;

	@Override
	public void onLoadComplete() {

		updater = new SelfUpdater(this);

		SharedPreferences prefs = getPreferences(0);
		
		maxLevel = prefs.getInt(PREF_MAX_LEVEL, 1);
		
		updater.preferences.read(prefs);

		updater.setUrl(updateUrl);

		updater.checkUpdateScheduled();

	}

	@Override
	public Engine onLoadEngine() {
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
				this.camera).setNeedsSound(true));
	}

	@Override
	public void onLoadResources() {

		TextureRegionFactory.setAssetBasePath("gfx/");

		backgroundTexture = new Texture(1024, 1024, TextureOptions.DEFAULT);
		backgroundRegion = TextureRegionFactory.createFromAsset(
				backgroundTexture, this, "bg.png", 0, 0);

		mEngine.getTextureManager().loadTexture(backgroundTexture);

		mEngine.getTextureManager().loadTexture(backgroundTexture);

		titleTexture = new Texture(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		if (titleLp == null) {
			titleLp = new LocalizedPath("text/title%s.png", "_"
					+ LocalizedPath.LOWERCASE_FILENAME, null, null)
					.setPathChecker(new LocalizedPath.PathChecker() {

						@Override
						public boolean check(final String path) {
							return checkLocalizedString(path);
						}
					});
		}

		String trText = titleLp.getLocalizedPath();

		titleRegion = TextureRegionFactory.createFromAsset(titleTexture, this,
				trText, 0, 0);

		mEngine.getTextureManager().loadTexture(titleTexture);

		fontTexture = new Texture(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		mEngine.getTextureManager().loadTexture(fontTexture);

		font = new Font(fontTexture, Typeface.create(Typeface.DEFAULT,
				Typeface.BOLD), 48, true, Color.RED);

		fontHalfTexture = new Texture(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		mEngine.getTextureManager().loadTexture(fontHalfTexture);

		fontHalf = new Font(fontHalfTexture, Typeface.create(Typeface.DEFAULT,
				Typeface.BOLD), 38, true, Color.YELLOW);

		mEngine.getFontManager().loadFont(font);
		mEngine.getFontManager().loadFont(fontHalf);

		panelTexture = new Texture(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		panelRegion = TextureRegionFactory.createFromAsset(panelTexture, this,
				"panel.png", 0, 0);

		mEngine.getTextureManager().loadTexture(panelTexture);

		panelTextureX = new Texture(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		panelRegionX = TextureRegionFactory.createFromAsset(panelTextureX,
				this, "panelx.png", 0, 0);

		mEngine.getTextureManager().loadTexture(panelTextureX);

		lightOnTexture = new Texture(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		lightOnRegion = TextureRegionFactory.createFromAsset(lightOnTexture,
				this, "lighton.png", 0, 0);

		mEngine.getTextureManager().loadTexture(lightOnTexture);

		lightOffTexture = new Texture(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		lightOffRegion = TextureRegionFactory.createFromAsset(lightOffTexture,
				this, "lightoff.png", 0, 0);

		mEngine.getTextureManager().loadTexture(lightOffTexture);

		SoundFactory.setAssetBasePath("mfx/");

		try {
			sfxToggle = SoundFactory.createSoundFromAsset(
					this.mEngine.getSoundManager(), this, "cursor.wav");
		} catch (final IOException e) {
			Debug.e("Error loading trigger.wav", e);
		}

		try {
			sfxDone = SoundFactory.createSoundFromAsset(
					this.mEngine.getSoundManager(), this, "done.ogg");
		} catch (final IOException e) {
			Debug.e("Error loading done.ogg", e);
		}

		try {
			sfxRejected = SoundFactory.createSoundFromAsset(
					this.mEngine.getSoundManager(), this, "rejection.mp3");
		} catch (final IOException e) {
			Debug.e("Error loading rejection.mp3", e);
		}

	}

	public boolean checkLocalizedString(String string) {

		Texture titleTexture = new Texture(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		TextureRegion tr = TextureRegionFactory.createFromAsset(titleTexture,
				this, string, 0, 0);

		if (tr.getWidth() <= 0)
			return false;

		return true;
	}

	@Override
	public Scene onLoadScene() {
		return new MainMenuScene(this).getScene();
	}

	@Override
	public void onGameResumed() {
		gameScene.onResumed();
	}

	@Override
	public void onGamePaused() {
		gameScene.onPaused();
		updateMaxLevel(0);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (gameScene.onKeyDown(keyCode, event))
			return true;
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (gameScene.onKeyUp(keyCode, event))
			return true;
		return super.onKeyUp(keyCode, event);
	}

	public void updateMaxLevel(int level) {

		if (level > maxLevel)
			maxLevel = level;

		SharedPreferences.Editor editor = getPreferences(0).edit();
		editor.putInt(PREF_MAX_LEVEL, maxLevel);
		
		updater.preferences.write(editor);
		
		editor.commit();
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onSetContentView() {

		LinearLayout layout = new LinearLayout(this);

		layout.setOrientation(LinearLayout.VERTICAL);

		RenderSurfaceView renderView = new RenderSurfaceView(this);

		LayoutParams renderViewparams = new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

		layout.addView(renderView, renderViewparams);

		super.setContentView(layout);

		this.mRenderSurfaceView = renderView;

		this.mRenderSurfaceView.setEGLConfigChooser(false);
		this.mRenderSurfaceView.setRenderer(this.mEngine);
	}

}
